﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Autofac.Features.OwnedInstances;

namespace BuildBox.Server
{
    public partial class MainService : ServiceBase
    {
        private readonly Func<Owned<BuildBoxServer>> _serverFactory;
        private Owned<BuildBoxServer> _server;

        public MainService(Func<Owned<BuildBoxServer>> serverFactory)
        {
            InitializeComponent();
            _serverFactory = serverFactory;
        }

        protected override void OnStart(string[] args)
        {
            _server = _serverFactory();
            _server.Value.Start();
        }

        protected override void OnStop()
        {
            if (_server != null)
            {
                _server.Value.Stop();
                _server.Dispose();
                _server = null;
            }
        }
    }
}
